package com.example.demo.config;

import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.example.demo.service.IAfshopUserCouponService;
import com.example.demo.service.impl.AfshopUserCouponServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/*
 *
 *   @Author: sjy
 *   @Date: 2024/5/9-05-09-11:22
 *   人脸人体识别配置
 */
@Slf4j
@Data
@Configuration
//从配置文件中获取配置
@ConfigurationProperties(prefix = "aliyun")
public class FaceConfig {
    private String accessKeyId;
    private String accessKeySecret;

    // 这里只是以ocr为例，其他能力请使用相应类目的包下面的Client类
    public Client createClient(String url) throws Exception {
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
        // 参考：https://help.aliyun.com/document_detail/143103.html
        config.endpoint = url;
        return new Client(config);
    }

    /**
     * 将指定图片模板合并到人脸数据库。
     *
     * @param templateUrl 需要合并的图片模板的URL。
     * @return 返回合并后模板的ID。
     * @throws Exception 如果合并过程中出现任何错误，则抛出异常。
     */
    private String faceMerge(String templateUrl) throws Exception {
        // 创建客户端以用于后续的API调用
        Client client = createClient("facebody.cn-shanghai.aliyuncs.com");
        // 准备合并请求，设置图片模板URL
        AddFaceImageTemplateRequest addFaceImageTemplateRequest = new AddFaceImageTemplateRequest().setImageURL(templateUrl);
        // 配置运行时选项
        RuntimeOptions runtime = new RuntimeOptions();
        // 执行合并请求，并获取响应
        AddFaceImageTemplateResponse addFaceImageTemplateResponse = client.addFaceImageTemplateWithOptions(addFaceImageTemplateRequest, runtime);
        // 解析响应，提取模板ID
        Map<String, Object> stringObjectMap = TeaModel.buildMap(addFaceImageTemplateResponse);
        String templateId = (String) stringObjectMap.get("templateId");
        return templateId;
    }


    /**
     * 合并图像和模板
     * @param srcImageUrl 合成图片地址
     * @param templateUrl 模板图片地址
     * @throws Exception 异常
     */
    public Map faceMerge(String srcImageUrl, String templateUrl) throws Exception {
        // 创建客户端
        Client client = createClient("facebody.cn-shanghai.aliyuncs.com");
        // 创建MergeImageFaceRequest对象，用于设置合并图像面部的请求参数
        com.aliyun.facebody20191230.models.MergeImageFaceRequest request =
                new com.aliyun.facebody20191230.models.MergeImageFaceRequest()
                .setImageURL(srcImageUrl)
                .setTemplateId(templateUrl);
        log.info("srcImageUrl:{}",srcImageUrl);
        log.info("templateUrl:{}",templateUrl);
        // 创建RuntimeOptions对象，用于设置调用接口时的运行时选项
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        // 使用指定的请求和运行时选项来执行合并图像面部的请求
        MergeImageFaceResponse mergeImageFaceResponse = client.mergeImageFaceWithOptions(request, runtime);

        Map<String, Object> stringObjectMap = TeaModel.buildMap(mergeImageFaceResponse);
        System.out.println("stringObjectMap = " + stringObjectMap);
        return stringObjectMap;
    }


    //人脸动漫画
    public Map faceToAnimated(String faceImg) throws Exception {
        com.aliyun.facebody20191230.Client client = createClient("facebody.cn-shanghai.aliyuncs.com");
        com.aliyun.facebody20191230.models.GenerateHumanAnimeStyleRequest generateHumanAnimeStyleRequest = new com.aliyun.facebody20191230.models.GenerateHumanAnimeStyleRequest()
                .setImageURL(faceImg)
                .setAlgoType("hongkong");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        // 复制代码运行请自行打印 API 的返回值
        GenerateHumanAnimeStyleResponse generateHumanAnimeStyleResponse = client.generateHumanAnimeStyleWithOptions(generateHumanAnimeStyleRequest, runtime);
        Map<String, Object> stringObjectMap = TeaModel.buildMap(generateHumanAnimeStyleResponse);
        System.out.println("stringObjectMap = " + stringObjectMap);
        return stringObjectMap;
    }

    @Bean
    public IAfshopUserCouponService afshopUserCouponService() {
        return new AfshopUserCouponServiceImpl();
    }

}
